package com.siyoumi.app.modules.user.admin;

import com.siyoumi.app.entity.SysAbc;
import com.siyoumi.app.entity.SysSms;
import com.siyoumi.app.modules.app_book.vo.VaBookSetting;
import com.siyoumi.app.modules.app_book.vo.VaBookSettingTxt00;
import com.siyoumi.app.modules.sms.service.SvcSms;
import com.siyoumi.app.modules.user.service.SvcSaleSetting;
import com.siyoumi.app.modules.user.vo.VoSaleSetting;
import com.siyoumi.app.modules.user.vo.VoSaleSettingTxt00;
import com.siyoumi.app.service.SysAbcService;
import com.siyoumi.component.XApp;
import com.siyoumi.component.http.InputData;
import com.siyoumi.component.http.XHttpContext;
import com.siyoumi.controller.AdminApiController;
import com.siyoumi.util.XJson;
import com.siyoumi.util.XReturn;
import com.siyoumi.util.XStr;
import com.siyoumi.validator.XValidator;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/xadmin/user/sale__setting")
public class user__sale__setting
        extends AdminApiController {
    @GetMapping()
    public XReturn index() {
        setPageTitle("提现配置");

        SysAbcService svcAbc = SysAbcService.getBean();
        String uix = "sale_setting";
        SysAbc entity = svcAbc.getEntityByUix(uix);
        if (entity == null) {
            entity = new SysAbc();
            entity.setAbc_app_id(XHttpContext.getAppId());
            entity.setAbc_x_id(XHttpContext.getX());
            entity.setAbc_table(uix);
            entity.setAbc_uix(uix);
            entity.setAutoID();

            svcAbc.save(entity);
            entity = svcAbc.getEntityByUix(uix);
        }
        Map<String, Object> data = new HashMap<>();

        HashMap<String, Object> dataAppend = new LinkedHashMap<>();
        dataAppend.put("id", entity.getKey());
        //合并
        data = entity.toMap();
        data.putAll(dataAppend);
        //
        VoSaleSettingTxt00 txt00 = SvcSaleSetting.getBean().getSettingTxt00();
        data.put("abc_txt_00", txt00);


        getR().setData("data", data);

        return getR();
    }


    @Transactional
    @PostMapping("/save")
    public XReturn save(@Validated() VoSaleSetting vo, BindingResult result) {
        //统一验证
        XValidator.getResult(result);
        //
        XValidator.getResult(result);
        //
        List<String> ignoreField = new ArrayList<>();
        if (isAdminEdit()) {
            ignoreField.add("abc_id");
            ignoreField.add("abc_table");
        }

        InputData inputData = InputData.fromRequest();
        XReturn r = SysAbcService.getBean().saveEntity(inputData, vo, true, ignoreField);
        if (r.ok()) {
            SysAbc entity = r.getData("entity");
            SysAbcService.getBean().delEntityCache("sale_setting");
        }

        return r;
    }
}
